//
//  ByGoodsIndexRecommendVC.m
//  mijifen_ios
//
//  Created by mc on 2017/11/6.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByGoodsIndexRecommendVC.h"
// cell
#import "ByCellGoodsIndexRecommend.h"

@interface ByGoodsIndexRecommendVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *items;
@end

@implementation ByGoodsIndexRecommendVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"开通首页推荐";
    [self addLeftBackBarButtonItem];
    [self createViews];
    [self fetchData];
}

#pragma mark - 获取数据
- (void)fetchData{
    for (int i = 0; i < 10; i ++) {
        ByModelGoodsRecommend *model = [[ByModelGoodsRecommend alloc] init];
        model.price = @"356";
        model.term = @"12";
        [self.items addObject:model];
    }
    [self.tableView reloadData];
}

#pragma mark - getter
- (NSMutableArray *)items{
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}

#pragma mark - 创建视图
- (void)createViews{
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-50);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom);
        }
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    headerView.backgroundColor = BY_TABLE_COLOR;
    UILabel *alertLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 15, SCREEN_WIDTH, 20)];
    [headerView addSubview:alertLabel];
    alertLabel.text = @"平台将会通过大数据分析用户所在位置并置顶您的商品";
    alertLabel.font = FONT(14);
    alertLabel.textAlignment = NSTextAlignmentCenter;
    self.tableView.tableHeaderView = headerView;
    
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.items.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellGoodsIndexRecommend *cell = [ByCellGoodsIndexRecommend cellWithTableView:tableView];
    cell.model = self.items[indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ByCellGoodsIndexRecommend *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = YES;
    [self.tableView reloadData];
}

@end
